#!/bin/bash -x

edk2_repo_url="https://gitee.com/src-openeuler/edk2.git"
edk2_repo_branch="openEuler-24.03-LTS"
edk2_repo_commit="4b71bcc5f5e9ff09b42993d33463518ba699580e"
edk2_repo_dir="/dev/shm/openeuler-edk2"

edk2_source_tree_builder_container_name="openeuler-2403-builder"
edk2_source_tree_builder_image_tag="openeuler/openeuler:24.03-lts"
edk2_source_tree_builder_script="build-kata-ovmf-edk2-source-tree.sh"
edk2_source_tree_basename="edk2-edk2-stable202308"
edk2_source_tree_dir="/dev/shm/${edk2_source_tree_basename}"

if [ ! -e ${edk2_repo_dir} ]; then
  git clone ${edk2_repo_url} \
    -b ${edk2_repo_branch} \
    ${edk2_repo_dir}
  pushd ${edk2_repo_dir}
  git reset --hard ${edk2_repo_commit}
  popd # ${edk2_repo_dir}
fi

generate_edk2_source_tree () {
  echo "Generate edk2 source tree"

  shared_top_dir=$(dirname ${edk2_source_tree_dir})

  cp ${edk2_source_tree_builder_script} ${shared_top_dir}

  sed -i "s!MARK_YUM_PROXY!${https_proxy}!g" \
    ${shared_top_dir}/${edk2_source_tree_builder_script}
  sed -i "s!MARK_EDK2_REPO_DIR!${edk2_repo_dir}!g" \
    ${shared_top_dir}/${edk2_source_tree_builder_script}
  sed -i "s!MARK_EDK2_SOURCE_TREE_BASENAME!${edk2_source_tree_basename}!g" \
    ${shared_top_dir}/${edk2_source_tree_builder_script}
  sed -i "s!MARK_EDK2_SOURCE_TREE_TARBALL!${edk2_source_tree_dir}.tgz!g" \
    ${shared_top_dir}/${edk2_source_tree_builder_script}
  sed -i "s!MARK_EDK2_REPO_BRANCH!${edk2_repo_branch}!g" \
    ${shared_top_dir}/${edk2_source_tree_builder_script}
  sed -i "s!MARK_EDK2_REPO_COMMIT!${edk2_repo_commit}!g" \
    ${shared_top_dir}/${edk2_source_tree_builder_script}

  docker image pull ${edk2_source_tree_builder_image_tag}

  if [ $(docker ps -a | \
         grep -c "${edk2_source_tree_builder_container_name}") -eq 0 ]; then
    docker run \
      --name=${edk2_source_tree_builder_container_name} \
      -d \
      -v ${shared_top_dir}:${shared_top_dir} \
      ${edk2_source_tree_builder_image_tag} \
      sleep infinity
  else
    docker start ${edk2_source_tree_builder_container_name} || true
  fi
  docker exec -t ${edk2_source_tree_builder_container_name} \
    bash -c "${shared_top_dir}/${edk2_source_tree_builder_script} 1"
  docker exec -t ${edk2_source_tree_builder_container_name} \
    bash -c "${shared_top_dir}/${edk2_source_tree_builder_script} 2"
}

generate_edk2_source_tree

# 3. build ovmf
install_ovmf_deps () {
  echo "Install ovmf dependencies"

  sudo apt install -y make gcc g++ iasl \
    python3 python3-distutils uuid-dev

  command nasm --version > /dev/null
  if [ $? -ne 0 ]; then
    pushd /dev/shm/
    curl -fsLO https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.gz && \
      tar -xf nasm-2.15.05.tar.gz && \
      cd nasm-2.15.05 && \
      ./configure && \
      make -j"$(nproc)" && \
      sudo make install
    popd # /dev/shm/
  fi
}

install_ovmf_deps

build_install_ovmf () {
  echo "Build and Install kata ovmf"
  tar -zxf ${edk2_source_tree_dir}.tgz -C $(dirname ${edk2_source_tree_dir})

  pushd ${edk2_source_tree_dir}
  export PYTHON_COMMAND=$(which python3)
  make -C BaseTools/
  source edksetup.sh
  touch OvmfPkg/AmdSev/Grub/grub.efi
  build -b RELEASE -t GCC5 -a X64 -p OvmfPkg/AmdSev/AmdSevX64.dsc -n $(($(nproc) - 2))
  sudo mkdir -p /opt/kata/share/ovmf || true
  sudo cp Build/AmdSev/*/FV/OVMF.fd /opt/kata/share/ovmf/OVMFCSV.fd
  popd # ${edk2_source_tree_dir}
}

build_install_ovmf
